from datetime import date
from enum import Enum, unique
from typing import Optional

from sqlalchemy import Enum as SQLEnum
from sqlmodel import Field, SQLModel

from models.common import Pagination


@unique
class IdentityCredentialsType(str, Enum):
    """
    证件类型
    """

    # 中国大陆居民
    ID_CARD = "ID_CARD"
    RESIDENCE_PERMIT = "RESIDENCE_PERMIT"
    PASSPORT = "PASSPORT"


@unique
class Gender(str, Enum):
    """
    性别
    """

    MALE = "MALE"
    FEMALE = "FEMALE"


class PersonBase(SQLModel):
    """
    人员
    """

    first_name: str = Field(max_length=16, description="名称")
    last_name: str = Field(max_length=16, description="姓氏")
    gender: Gender = Field(
        sa_type=SQLEnum(Gender),  # type: ignore
        default=Gender.MALE,
        max_length=8,
        description="性别",
    )
    birth_date: date = Field(description="出生日期")

    id_type: IdentityCredentialsType = Field(
        sa_type=SQLEnum(IdentityCredentialsType),  # type: ignore
        default=IdentityCredentialsType.ID_CARD,
        max_length=16,
        description="证件类型",
    )

    id_number: str = Field(
        unique=True,
        max_length=32,
        description="证件号码",
    )


class Person(PersonBase, table=True):
    id: Optional[int] = Field(
        primary_key=True,
        sa_column_kwargs={"autoincrement": True},
        description="编号",
    )


class PersonCreate(PersonBase):
    pass


class PersonUpdate(PersonBase):
    pass


class PersonPublic(Person):
    pass


class PersonsPublic(SQLModel):
    items: list[PersonPublic]
    # total:int
    pagination: Pagination
